<?php

/**
 * 
 * @author yuxun
 *
 */
class ActivityCategoryService extends Service{
	
	public static function indexHeader(){
		return array(
				'id'              => array('text' => '序号', 'rule' => 'int'),
				'activity_id'     => array('text' => '大活动ID'),
				'activity_name'   => array('text' => '大活动名称'),
				'classify'        => array('text' => '类别'),
				'duration'        => array('text' => '持续时间(天)'),
				'create_user'     => array('text' => '创建人'),
				'create_time'     => array('text' => '创建时间'),
				'action'          => array('text' => '操作')
		);
	}
	
	public static function indexData($params){
		$modActivityCategory = new ModActivityCategory();
		$result = $modActivityCategory->page($params);
		
		$classifyList = self::classifyList();
		$userList = self::userList();
		$length = count($result['data']);
		$modOssActivity = new ModOssActivity();
		foreach($result['data'] as &$v){
			$type = 1;
			if($k == 0 && $length == 1){  //当前页只剩下一条数据时，删除之后需显示上一页的数据
				$type = 2;
			}
			$v['classify'] = $classifyList[$v['classify']];
			$v['create_user'] = $userList[$v['create_user']];
			$v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
			
			$activityObj = $modOssActivity->findAllByAttributes(array('category_id'=>$v['id']));
			if(!$activityObj){
				$v['action']        .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=activityCategory/edit').'&id='.$v['id'].'">编辑</a>';
				$v['action']        .= ' <a href="'.PubFunc::buildBackUrl('/oss/?r=activityCategory/delete',$type).'&id='.$v['id'].'" onclick="del()" confirm="是否要删除该条服信息?">删除</a>';
			}			
		}
		unset($v);
		return $result;
	}
	
	public static function doAdd($params){
		$user = AccAuth::me();
		$modActivityCategory = new ModActivityCategory();
		$modActivityCategory->game = $params['g'];
		$modActivityCategory->activity_id   = $params['activity_id'];
		$modActivityCategory->activity_name  = $params['activity_name'];
		$modActivityCategory->classify  = $params['classify'];
		$modActivityCategory->duration  = $params['duration'];
		$modActivityCategory->create_user   = $user['UserID'];
		$modActivityCategory->create_time   = time();
		$modActivityCategory->update_user   = $user['UserID'];
		$modActivityCategory->update_time   = time();
		$result = $modActivityCategory->save();
		return array('state' => true, 'msg' => '成功增加大活动');
	}
	
	public static function doEdit($id, $params){
		if(empty($id)) return array('state' => false, 'msg' => '非法参数');
		$user = AccAuth::me();
		$modActivityCategory = new ModActivityCategory();
		$update = array();
		$update['activity_id'] = $params['activity_id'];
		$update['activity_name'] = $params['activity_name'];
		$update['classify'] = $params['classify'];
		$update['duration'] = $params['duration'];
		$update['update_user']  = $user['UserID'];
		$update['update_time']  = time();
		$modActivityCategory->updateByPk($id, $update);
		return array('state' => true, 'msg' => '编辑成功');
	}
	
	
	public static function delete($id){
		if(empty($id)) return array('state' => false, 'msg' => '非法参数');
		$modActivityCategory = new ModActivityCategory();
		$modActivityCategory->deleteByPk($id);
		return array('state' => true, 'msg' => '成功删除');
	}
	
	public static function get($id){
		$modActivityCategory = new ModActivityCategory();
		$obj = $modActivityCategory->findByPk($id);
	
		if($obj){
			$info = $obj->getAttributes();
			return $info;
		}else{
			return false;
		}
	}
	
	
	/*
	 * 去重
	 */
	public static function repeat($id = 0, $field, $val,$g){
		$modActivityCategory = new ModActivityCategory();
		$conut = $modActivityCategory->repeat($id, $field, $val,$g);
		return $conut;
	}
	
	public static function classifyList(){
		return array(
				'1' => '主题活动',
				'2' => '日常活动',
		);
	}
	
	public static function upload($files){
		$file = new PubFile();
		$fileName = $files["file"]["name"];
		$suffixArr = array('xls','xlsx','xlsm'); //允许上传的文件后缀
		$check= $file::checkSuffix($fileName,$suffixArr);
		if($check){
			$ext = $file::getSuffix($fileName);
			$uploadDir = '../public/upload/activityCategory/'.date('Ym').'/';
			$uploadName = md5(microtime(true)).'.'.$ext;
			$upload = $file::uploadFile($files["file"],$uploadDir,$uploadName);
			if(!$upload){
				return array('status' => 0, 'msg' =>$upload,'data'=>'');
			}else{
				$path = dirname(dirname(dirname(dirname(__File__)))).'/public/upload/activityCategory/'.date('Ym').'/'.$uploadName;  //获取绝对路径
				return array('status' => 1, 'msg' =>'上传成功','data'=>$path);
			}
		}else{
			return array('status' => 0, 'msg' =>'上传文件后缀不允许!','data'=>'');
		}
	}
	
	public static function xlsAdd($data){
		$user = AccAuth::me();
		$modActivityCategory = new ModActivityCategory();
		unset($data[1]);
		unset($data[2]);
		unset($data[3]);
		unset($data[4]);
		 
		$add_data = array();
		$gid_array = $modActivityCategory->getGid();
		 
		$gid_in = array();
		foreach ($gid_array as $key=>$val){
			$gid_in[] = $val['activity_id'];
		}
		$game = PubBase::getCurrentGame();
		$all_num = count($data);
		$success_num = 0;
		$error_num = 0;
		foreach($data as $v){
			if(in_array($v['A'], $gid_in)){  //已经存在
				$error_num++;
				continue;
			}
			$d = array();
			$d['game'] = $game;
			$d['activity_id'] = $v['A'];
			$d['activity_name'] = $v['B'];
			$d['classify'] = $v['C'];
			$d['duration'] = $v['D'];
			$d['create_user'] = $user['UserID'];
			$d['create_time'] = time();
			$d['update_user'] = $user['UserID'];
			$d['update_time'] = time();
			array_push($add_data, $d);
		}
		$success_num = $all_num - $error_num;
		$msg = '导入成功 '.$success_num.' 条，失败 '.$error_num.' 条';
		$result = $modActivityCategory->addmuch($add_data);
		return array('status' => 1, 'msg' =>$msg,'data'=>'');
	}
	
	
	public static function getActivity($classify){
		if(!$classify){
			$result = array('status' => 0,'msg' => '参数错误','data'=> '');
			return $result;
		}
		$where = array();
		$game = PubBase::getCurrentGame();
		$where['g'] = $game;
		$where['classify'] = $classify;
		$modActivityCategory = new ModActivityCategory();
		$data = $modActivityCategory->select($where);
		$result = array('status' => 1,'msg' => '查询成功','data'=> $data);
		return $result;
	}
	
	public static function getActivityInfo($category_id){
		if(!$category_id){
			$result = array('status' => 0,'msg' => '参数错误','data'=> '');
			return $result;
		}
		$data = self::get($category_id);
		$result = array('status' => 1,'msg' => '查询成功','data'=> $data);
		return $result;
	}
	


}